<?php

/***
 * Creating html tag
* @author DuongNV
* @copyright Tan MA JSC
* @category common function
* @version 1.1
*/

/**
 * create select tag
 * @param $id is the name of tag
 * @param $class is class css of tag
 * @param $value is colum name use for value of select tag
 * @param $text is colum name use for text of select tag
 * @param $tableName is table name to select
 * @param $condition is codition string to select query string
 * @param $selected is value selected of select tag
 * @return $selectTag is a string content select html tag
 */
function createSelectTag($id, $class, $value, $text, $tableName, $condition, $selected, $onchange = "") {

    $selectTag = "<select id=\"" . $id . "\" class=\"" . $class . "\" name=\"" . $id . "\" onchange=\"$onchange\">";

    // creare array colum name want to select
    $colum = array($value, $text);

    // define new DAO (Reader) and select array object
    $dbBean = new DBAccess();

    $arrayObject = $dbBean->selectManyByCause(
                    array($value, $text),
                    $tableName,
                    $condition);

    //selectManyByCause( $colum, $tableName, $condition, " ORDER BY `ID` DESC ");

    // fill option with selected
    foreach ( $arrayObject as $object ) {
        if ($object[$value] == $selected) {
            $selectTag .= "<option value=\"" . $object[$value] . "\" selected=\"true\">";
            $selectTag .= $object[$text];
            $selectTag .= "</option>";
        } else {
            $selectTag .= "<option value=\"" . $object[$value] . "\">";
            $selectTag .= $object[$text];
            $selectTag .= "</option>";
        }
    }

    // colse select tag
    $selectTag .= "</select>";

    return $selectTag;
}

?>
